//
//  YFClubSettingVC.m
//  SquashSpark
//
//  Created by Coollang on 2017/12/6.
//  Copyright © 2017年 Coollang-YF. All rights reserved.
//

#import "YFClubSettingVC.h"
#import "JCClubSetupVC.h"
#import "JCClubApi.h"
#import "JCSetClubPasswordVC.h"
#import "JCEditClubInformationVC.h"

@interface YFClubSettingVC ()
@property (nonatomic, strong) UITableView *tabelView;
@end

@implementation YFClubSettingVC
- (void)loadView {
    self.tabelView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStyleGrouped];
    self.tabelView.backgroundColor = kSeparationLineColor;
    self.tabelView.separatorStyle = UITableViewCellSelectionStyleNone;
    self.tabelView.delegate = self;
    self.tabelView.dataSource = self;
    self.view = self.tabelView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self loadDataItems];
    if ([[User getUseInfo] clubUserType] == ClubUserTypeGeneral) {
        [self creatLogOutButton];
    }
    self.tabelView.sectionFooterHeight = 1;
}

- (void)loadDataItems {
    if ([[User getUseInfo] clubUserType] != ClubUserTypeAdmin) {
        return;
    }
    __weak typeof(self)weakSelf = self;
    if (self.groups.count > 0) {
        [self.groups removeAllObjects];
    }
    UserGroup *group0 = [[UserGroup alloc] init];
    group0.headHeight = 0.00000001;
    UserArrowItem *item0_0 = [UserArrowItem itemWithIcon:nil andTitle:@"Club information" andTarget:nil];
    [item0_0 setBlock:^(UserItem *itemT, NSIndexPath *indexpath) {
        JCEditClubInformationVC *editClubVC = [JCEditClubInformationVC new];
        editClubVC.clubID = [User getUseInfo].adminClubs.firstObject.clubID;
        [self.navigationController pushViewController:editClubVC animated:YES];
    }];
    
    UserArrowItem *item0_1 = [UserArrowItem itemWithIcon:nil andTitle:@"Movement Plan" andTarget:nil];
    [item0_1 setBlock:^(UserItem *itemT, NSIndexPath *indexpath) {
        UIStoryboard *sb = [UIStoryboard storyboardWithName:@"ClubsExtension" bundle:[NSBundle mainBundle]];
        JCClubSetupVC *setupVC = [sb instantiateViewControllerWithIdentifier:@"JCClubSetupVC"];
        NSString *clubID;
        if ([[User getUseInfo] clubUserType] == ClubUserTypeAdmin) {
            clubID = [User getUseInfo].adminClubs.firstObject.clubID;
        }else if ([[User getUseInfo] clubUserType] == ClubUserTypeGeneral){
            clubID = [User getUseInfo].joinClubs.firstObject.clubID;
        }
        setupVC.clubID = clubID;
//        setupVC.defaultValueItem = _clubModel.clubWeekTarget;
        [self.navigationController pushViewController:setupVC animated:YES];
    }];
    
    UserArrowItem *item0_2 = [UserArrowItem itemWithIcon:nil andTitle:@"Setting Password" andTarget:nil];
    [item0_2 setBlock:^(UserItem *itemT, NSIndexPath *indexpath) {
        JCSetClubPasswordVC *setVC = [JCSetClubPasswordVC new];
        setVC.clubID = [User getUseInfo].adminClubs.firstObject.clubID;
        [weakSelf.navigationController pushViewController:setVC animated:YES];
    }];
    
    [group0.items addObjectsFromArray:@[item0_0,item0_1,item0_2]];
    [self.groups addObject:group0];
}


- (void)creatLogOutButton {
    UIButton *logoutBtn = [UIButton creatButtonTitle:@"Quit Club" bgColor:kBgThemeColor textFont:kOpenSansFont(OpenSansRegular, 16) textColor:kTRedEEText];
    logoutBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    logoutBtn.titleEdgeInsets = UIEdgeInsetsMake(0, 16, 0, 0);
    logoutBtn.frame = CGRectMake(0, 0, kYFScreenWidth, 50);
    [logoutBtn addTarget:self action:@selector(logOutBtnClick) forControlEvents:UIControlEventTouchUpInside];
    self.tabelView.tableFooterView = logoutBtn;
}

- (void)logOutBtnClick {
    __weak typeof(self)weakSelf = self;
    FUIAlertView *alertv = [FUIAlertView alertWithTitle:@"Prompt" message:@"You opration will withdraw from the current club and withdraw from the club will lose the information and data already available" cancelTitle:@"Reconsider" confirmTitle:@"Confirm"];
    [alertv setOnOkAction:^{
        NSString *clubID = [User getUseInfo].joinClubs.firstObject.clubID;
        [weakSelf.hud showWithMaskType:WSProgressHUDMaskTypeClear];
        [JCClubApi quitClubForClubID:clubID complete:^(NSError *error) {
            if (error) {
                [weakSelf.hud showErrorWithString:error.domain dimissAfterSecond:2];
            }else{
                [weakSelf.hud showSuccessWithStatus:@"Quit Success!" dimissAfterSecond:2];
                weakSelf.tabelView.tableFooterView = nil;
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    [self.navigationController popViewControllerAnimated:YES];
                });
            }
        }];
    }];
    [alertv show];
}


@end
